home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / glibc108.gz / glibc108 / glibc-1.08.1 / manual / examples / sigusr.c < prev    next >
C/C++ Source or Header  |  1994-02-16  |  1KB  |  62 lines

  1. /*@group*/
  2. #include <signal.h>
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. /*@end group*/
  7.  
  8. /* When a @code{SIGUSR1} signal arrives, set this variable.  */
  9. volatile sig_atomic_t usr_interrupt = 0;
  10.  
  11. void 
  12. synch_signal (int sig)
  13. {
  14.   usr_interrupt = 1;
  15. }
  16.  
  17. /* The child process executes this function. */
  18. void 
  19. child_function (void)
  20. {
  21.   /* Perform initialization. */
  22.   printf ("I'm here!!!  My pid is %d.\n", (int) getpid ());
  23.  
  24.   /* Let parent know you're done. */
  25.   kill (getppid (), SIGUSR1);
  26.  
  27.   /* Continue with execution. */
  28.   puts ("Bye, now....");
  29.   exit (0);
  30. }
  31.  
  32. int
  33. main (void)
  34. {
  35.   struct sigaction usr_action;
  36.   sigset_t block_mask;
  37.   pid_t child_id;
  38.  
  39.   /* Establish the signal handler. */
  40.   sigfillset (&block_mask);
  41.   usr_action.sa_handler = synch_signal;
  42.   usr_action.sa_mask = block_mask;
  43.   usr_action.sa_flags = 0;
  44.   sigaction (SIGUSR1, &usr_action, NULL);
  45.  
  46.   /* Create the child process. */
  47.   child_id = fork ();
  48.   if (child_id == 0)
  49.     child_function ();        /* Does not return.  */
  50.  
  51. /*@group*/
  52.   /* Busy wait for the child to send a signal. */
  53.   while (!usr_interrupt)
  54.     ;
  55. /*@end group*/
  56.  
  57.   /* Now continue execution. */
  58.   puts ("That's all, folks!");
  59.  
  60.   return 0;
  61. }
  62.